#include <stdio.h>
#include "client.h"
#include "staff.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include "model.h"
//客户端登录
void client_login()
{
}
//展示进入客户端登录的菜单
void menu_login()
{
	printf("*************************\n");
	printf("1.普通用户登录\n");
	printf("2.管理员登录\n");
	printf("3.退出\n");
	printf("*************************\n");
}

//用户/管理员登录
int login(int sockfd, int msg_type)
{

	msg_t msg;
	printf("请输入用户名>> ");
	scanf("%s", msg.staff_info.name);
	getchar();

	printf("请输入密码>> ");
	scanf("%s", msg.staff_info.passwd);
	msg.type = msg_type;
	if (send(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		ERR("fail to send");
	}
	memset(&msg, 0, sizeof(msg));
	if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		ERR("fail to send");
	}
	printf("type:%d,msg:%s\n", msg.type, msg.msg);

	if (msg.type == SERVER_OK)
	{
		printf("*登录成功*\n");
		return 1;
	}
	else if (msg.type == SERVER_ERR)
	{
		printf("登录失败:%s\n", msg.msg);
		return 0;
	}
	return 1;
}
void user_menu(msg_t msg, int sockfd)
{
	/*	int input;
	printf("user_menu\n");
	printf("亲爱的用户，欢迎登录员工管理系统！\n");
	printf("************************************\n");
	printf("*******1.查询 2.修改 3.退出**********\n");
	printf("************************************\n");
	
	printf("请输入您的选择：");
	scanf("%d",&input);
	getchar();

	switch(input)
	{
		case 1:
			printf("工号    用户类型     姓名      密码      年龄     手机号      地址       入职年月       等级      薪资\n");	
			printf("%d        %d         %s         %s         %d       %s         %s           %s           %d        %.lf",
				msg.staff_info.no , msg.staff_info.user_type , msg.staff_info.name , msg.staff_info.passwd , msg.staff_info.age ,
				msg.staff_info.phone , msg.staff_info.addr , msg.staff_info.join_at , msg.staff_info.level , msg.staff_info.salary);
			break;
		case 2:
			printf("请输入要修改的选项：(修改其他信息请联系管理员)\n");

			printf("********************************************\n");
			printf("******1.家庭住址 2.电话 3.密码 4.退出*******\n");
			printf("********************************************\n");

			printf("请输入选择: ");
			scanf("%d",&input);
			getchar();
			
			switch(input)
			{
				case 1:
					printf("请输入家庭住址：");
					scanf("%s",msg.staff_info.addr);
					
					send(sockfd,&msg,sizeof(msg),0);
					recv(sockfd,&msg,sizeof(msg),0);
					break;
				case 2:
					printf("请输入电话：");
					scanf("%s",msg.staff_info.addr);
					
					send(sockfd,&msg,sizeof(msg),0);
					recv(sockfd,&msg,sizeof(msg),0);
					break;
				case 3:
					printf("请输入密码：");
					scanf("%s",msg.staff_info.addr);
					
					send(sockfd,&msg,sizeof(msg),0);
					recv(sockfd,&msg,sizeof(msg),0);
					break;
				case 4:
					printf("byebye!\n");
					return 1;
				default:
					printf("您输入有误，请重新输入！\n");
					break;
			
			}
		case 3：
			printf("byebye!\n");
			return 1;
			
			}
*/
}
void admin_menu(int sockfd)
{
	int input;

	printf("亲爱的管理员，欢迎您登录员工管理系统！！！\n");

	//管理员的增删改查
	while (1)
	{
		printf("********************************************************************\n");
		printf("1：查询  2：修改 3：添加用户  4：删除用户  5：查询历史记录   6：退出\n");
		printf("********************************************************************\n");
		printf("请输入您的选择：>> ");

		scanf("%d", &input);
		getchar();

		switch (input)
		{
		case 1:
			search_msg(sockfd);
			break;
		case 2:
			modify_msg(sockfd);
			break;
		case 3:
			add_msg(sockfd);
			break;
		case 4:
			del_msg(sockfd);
			break;
		case 5:
			his_msg(sockfd);
			break;
		case 6:
			printf("byebye!\n");
			return;
		default:
			printf("您输入有误，请重新输入\n");
			break;
		}
	}
}
//查询
void search_msg(int sockfd)
{

	int input;
	msg_t msg;
	//管理员查询

	while (1)
	{
		printf("********************************\n");
		printf("1.按人名查找  2.查找所有  3.退出\n");
		printf("********************************\n");
		printf("请输入您的选择：>> ");
		scanf("%d", &input);
		getchar();
		switch (input)
		{
		case 1:
			/*	printf("请输入您要查找的姓名：>> "); 
				scanf("%s",msg.staff_info.name);
				getchar();

				if(send(sockfd,&msg,sizeof(msg),0) == -1)
				{
				ERR("fail to send");
				}
				res = recv(sockfd,&msg,sizeof(msg),0)
				if(res == -1)
				{
				printf("%s\n",msg.msg);
				return 0;
				}else{
				printf("recv成功!\n");
				}
				if(msg.type == SERVER_FINNISH)
				{
				printf("已查完全部\n");
				return 1;
				}

				printf("工号    用户类型     姓名      密码      年龄     手机号      地址       入职年月       等级      薪资\n");	
				printf("%d        %d         %s         %s         %d       %s         %s           %s           %d        %.lf",
				msg.staff_info.no , msg.staff_info.user_type , msg.staff_info.name , msg.staff_info.passwd , msg.staff_info.age ,
				msg.staff_info.phone , msg.staff_info.addr , msg.staff_info.join_at , msg.staff_info.level , msg.staff_info.salary);
				break;

*/
		case 2:
			memset(&msg, 0, sizeof(msg));
			msg.type = TYPE_ADMIN_SEARCH;

			printf("\n");
			printf("%4s %8s %10s %10s %10s %20s %10s %10s %4s %4s %4s\n",
                    "工号", "用户类型", "姓名", "密码", "年龄", "手机号", "地址", "入职年月",  "职位", "等级", "薪资");

			if (send(sockfd, &msg, sizeof(msg), 0) == -1)
			{
				ERR("fail to send");
			}
			while (1)
			{
				if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
				{
					ERR("recv");
				}

				if (msg.type == SERVER_FINNISH)
				{
					printf("已查完全部\n");
					return;
				}


				printf("%4d %8d %10s %10s %4d %20s %10s %10s %4s %4d %6.2f\n",
					   msg.staff_info.no, msg.staff_info.user_type, msg.staff_info.name, msg.staff_info.passwd, msg.staff_info.age,
					   msg.staff_info.phone, msg.staff_info.addr, msg.staff_info.join_at, msg.staff_info.position, msg.staff_info.level, msg.staff_info.salary);
			}

		case 3:
			printf("byebye!\n");
			return;
		default:
			printf("您输入有误，请重新输入\n");
			break;
		}
	}
}

//修改
void modify_msg(int sockfd)
{
	int input;
	msg_t msg;
	msg.type = TYPE_ADMIN_SEARCH;
    int userid = 0;
	printf("请输入您要修改的工号：");
	scanf("%d", &userid);
    msg.staff_info.no = userid;

	if (send(sockfd,&msg,sizeof(msg),0) == -1)
	{
		ERR("fail to send");
	}
	
	if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		ERR("recv");
	}

	if (msg.type == SERVER_ERR)
	{
		printf("输入的工号有误!\n");
		return ;
	}

	msg.type = TYPE_ADMIN_MODIFY;
    msg.staff_info.no = userid;

	printf("*****************************请输入要修改的选项*******************************\n");
	printf("******************************************************************************\n");
	printf("1.姓名 2.年龄 3.家庭住址 4.电话 5.职位 6.工资 7.入职年月 8.评级 9.密码 10.退出\n");
	printf("******************************************************************************\n");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		printf("请输入要修改的姓名：");
		scanf("%s", msg.staff_info.name);
		getchar();
		sprintf(msg.msg,"name");
		break;

	case 2:
		printf("请输入要修改的年龄：");
		scanf("%d", &msg.staff_info.age);
		sprintf(msg.msg,"age");
		break;

	case 3:
		printf("请输入要修改的家庭住址：");
		scanf("%s", msg.staff_info.addr);
		getchar();
		sprintf(msg.msg,"addr");
		break;

	case 4:
		printf("请输入要修改的电话：");
		scanf("%s", msg.staff_info.phone);
		getchar();
		sprintf(msg.msg,"phone");
		break;

	case 5:
		printf("请输入要修改的职位：");
		scanf("%s", msg.staff_info.position);
		getchar();
		sprintf(msg.msg,"position");
		break;

	case 6:
		printf("请输入要修改的工资：");
		scanf("%lf", &msg.staff_info.salary);
		sprintf(msg.msg,"salary");
		break;

	case 7:
		printf("请输入要修改的入职年月：");
		scanf("%s", msg.staff_info.join_at);
		getchar();
		sprintf(msg.msg,"join_at");
		break;

	case 8:
		printf("请输入要修改的评级：");
		scanf("%d", &msg.staff_info.level);
		sprintf(msg.msg,"level");
		break;

	case 9:
		printf("请输入要修改的密码：");
		scanf("%s", msg.staff_info.passwd);
		getchar();
		sprintf(msg.msg,"passwd");
		break;

	case 10:
		printf("byebye!\n");
		return ;

	default:
		printf("您输入的有误，请重新输入！\n");
		break;

	}

    if (send(sockfd,&msg,sizeof(msg),0) == -1)
	{
		ERR("fail to send");
	}
	
	if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		ERR("recv");
	}

    if (msg.type == SERVER_OK) 
    {
        printf("修改成功\n");
    } else {
        printf("修改失败\n");
    }
	
}

//管理员添加
int add_msg(int sockfd)
{
	msg_t msg;

	char i;
	printf("热烈欢迎新员工！！！\n");
	while (1)
	{
		printf("请输入员工工号：");
		scanf("%d", &msg.staff_info.no);
		getchar();

		printf("请输入员工姓名：");
		scanf("%s", msg.staff_info.name);
		getchar();

		printf("请输入密码：");
		scanf("%s", msg.staff_info.passwd);
		getchar();

		printf("请输入年龄：");
		scanf("%d", &msg.staff_info.age);
		getchar();

		printf("请输入手机号：");
		scanf("%s", msg.staff_info.phone);
		getchar();

		printf("请输入地址：");
		scanf("%s", msg.staff_info.addr);
		getchar();

		printf("请输入入职年月：");
		scanf("%s", msg.staff_info.join_at);
		getchar();

		printf("请输入等级(1-5),5是最高，新员工为1：");
		scanf("%d", &msg.staff_info.level);
		getchar();

		printf("请输入职位：");
		scanf("%s", msg.staff_info.position);
		getchar();

		printf("请输入薪资：");
		scanf("%lf", &msg.staff_info.salary);
		getchar();

		printf("是否设为管理员y/n? ");
		scanf("%c", &i);
		getchar();
		if (i == 'y' || i == 'Y')
		{
			msg.staff_info.user_type = 1;
		}
		else
		{
			msg.staff_info.user_type = 2;
		}

		msg.type = TYPE_ADD;
		if (send(sockfd, &msg, sizeof(msg), 0) == -1)
		{
			ERR("fail to send");
		}
		if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
		{
			printf("fail to recv\n");
			return 0;
		}
		else
		{
			printf("msg:%s\n", msg.msg);
		}

		printf("是否继续添加y/n? ");
		getchar();

		if (i == 'y' || i == 'Y')
		{
			printf("请继续添加\n");
		}
		else if (i == 'n' || i == 'N')
		{
			printf("退出\n");
			break;
		}
	}
	return 1;
}
//管理员删除
void del_msg(int sockfd)
{
	msg_t msg;
	msg.type = TYPE_DELETE;
	printf("请输入您要删除的工号：");
	scanf("%d", &msg.staff_info.no);
	getchar();

	printf("请输入您要删除的姓名：");
	scanf("%s", msg.staff_info.name);
	getchar();

	if (send(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		ERR("fail to send");
	}

	memset(&msg, 0, sizeof(msg));
	if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
	{
		printf("msg:%s", msg.msg);
	}

	if (msg.type == SERVER_ERR)
	{
		printf("delete error: %s\n", msg.msg);
		return;
	}

	printf("删除成功\n");

	return;
}
//历史记录
void his_msg(int sockfd)
{
    msg_t msg;
    msg.type = TYPE_HISTORY;

        if (send(sockfd, &msg, sizeof(msg), 0) == -1)
    {
        ERR("fail to send");
    }

    while (1)
    {
        memset(&msg, 0, sizeof(msg));
        if (recv(sockfd, &msg, sizeof(msg), 0) == -1)
        {
            ERR("recv");
        }

        if (msg.type == SERVER_FINNISH)
        {
            printf("已查完全部\n");
            return;
        }

        printf("时间:%s 管理员:%s 操作:%s\n",msg.history.time, msg.history.name, msg.history.words);
    }
}

